package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet Filter implementation class ResponseFilter
 */
@WebFilter(filterName = "ResponseFilter", urlPatterns = { "/*" }, initParams = {
		@WebInitParam(name = "find", value = "Fachinformatiker"),
		@WebInitParam(name = "replace", value = "<b>Fachinformatiker</b>") })
public class ResponseFilter implements Filter {
	private FilterConfig fConfig;

	/**
	 * Default constructor.
	 */
	public ResponseFilter() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		ResponseWrapper wrapper = new ResponseWrapper(
				(HttpServletResponse) response);

		chain.doFilter(request, wrapper);
		PrintWriter out = response.getWriter();
		String content = wrapper.getContent();
		content = content.replace("Fachinformatiker",
				"<a href=\"http://www.google.de/\">Fachinformatiker</a>");
		content = content.replace(fConfig.getInitParameter("find"),
				fConfig.getInitParameter("replace"));
		out.write(content);
		out.close();

	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		this.fConfig = fConfig;
	}

}
